var enable=new Array();
var addMail = 1;
function evaluateSubmit(e, location) {  
    var key = null;  
  
    if (e.which) {          
        key = e.which; //normale Browser  
    } else {  
        key = e.keyCode; //IE  
    }  
      
    if (key == 13) { //ENTER  
        location();  
        return false; //Submit verhindern  
    } else {  
        return true;  
    }  
}  

function checkusername(str)
{
var answer = "";
if (str=="")
  {
  document.getElementById("usernamecheck").innerHTML="";
  enable[0] = false;
  return;
  } 
  
  if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//return xmlhttp.responseText;
			if (xmlhttp.responseText == "<img src='images/wrong.png' width='18' height='18'/> This username is already in use"){
				enable[0] = false;
			}else{
				enable[0] = true;
			}
			document.getElementById("usernamecheck").innerHTML=xmlhttp.responseText;
			enableButton();
		}
	  }
	xmlhttp.open("POST","scripts/user/userfunctions.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("value=checkusername;"+str);
  
  /*answer = ajax("scripts/user/userfunctions.php","value=checkusername;"+str);
  if (answer == "<img src='images/wrong.png' width='18' height='18'/> This username is already in use"){
		enable[0] = false;
	}else{
		enable[0] = true;
	}
	document.getElementById("usernamecheck").innerHTML=answer;
	enableButton();*/
}

function ajax(path,send){
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			return xmlhttp.responseText;
		}
	  }
	xmlhttp.open("POST",path,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(send);
}

function enableButton(){
	if (enable[0] && enable[1] && enable[2] && enable[3] 
	&& enable[4] && enable[6] == true){
		document.getElementById("finish").disabled = false;
	}else{
		document.getElementById("finish").disabled = true;
	}
}

function enableButton2(){
	if (enable[7] && enable[8] == true){
		document.getElementById("finish2").disabled = false;
	}else{
		document.getElementById("finish2").disabled = true;
	}
}

function DoesMatch(string1, string2)
{
	if (string1 == string2){
		DoesMatch = true;
		}else{
		DoesMatch = false;
		}
	return DoesMatch;
}
	
function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
   
function IsEmpty(aTextField) {
   if ((aTextField.length==0) ||
   (aTextField==null)) {
      return true;
   }
   else { return false; }
}

function passwordcheck(password, password2){
	if(IsEmpty(password)) 
   { 
      document.getElementById("passwordemptycheck").innerHTML="";
		enable[1] = false;
		return;
   } else {
		if (password.length >= 6){
			document.getElementById("passwordemptycheck").innerHTML="<img src='images/right.png' width='18' height='18'/>";
			enable[1] = true;
		}else{
			document.getElementById("passwordemptycheck").innerHTML="<img src='images/wrong.png' width='18' height='18'/> The password have to contain at least 6 chars";
			enable[1] = false;
		}
   }
   	if(IsEmpty(password2)) 
   { 
      document.getElementById("passwordcheck").innerHTML="";
		enable[2] = false;
		return;
   } else {
	if (password2 == password){
			document.getElementById("passwordcheck").innerHTML="<img src='images/right.png' width='18' height='18'/>";
			enable[2] = true;
		}else{
			document.getElementById("passwordcheck").innerHTML="<img src='images/wrong.png' width='18' height='18'/> The passwords doesn't match";
			enable[2] = false;
		}
   }
   enableButton();
}

function FieldEmpty(field,name){
	var response, number;
	switch(name)
	{
	case "prename":
	  response = "prenamecheck";
	  number = 3;
	  break;
	case "name":
	  response = "namecheck";
	  number = 4;
	  break;
	case "country":
	  response = "countrycheck";
	  number = 5;
	  break;
	case "city":
	  response = "citycheck";
	  number = 6;
	  break;
	case "twitter":
	  response = "twittercheck";
	  number = 11;
	  break;
	default:
	  response = "";
	  number = "";
	}
	if (IsEmpty(field)) {
		document.getElementById(response).innerHTML="<img src='images/wrong.png' width='18' height='18'/>The field '"+name+"' is empty";
		enable[number] = false;
	}else{
		document.getElementById(response).innerHTML="<img src='images/right.png' width='18' height='18'/>";
		enable[number] = true;
	}
	enableButton();
}

function emailvalidcheck(email,email2)
{
	var answer = "";
	if (IsEmpty(email))
	{
		document.getElementById("emailvalidcheck").innerHTML="";
		enable[7] = false;
		return;
	} 
	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//return xmlhttp.responseText;
			if (xmlhttp.responseText != "<img src='images/right.png' width='18' height='18'/> This e-mail adress is valid"){
				enable[7] = false;
			}else{
				enable[7] = true;
			}
			document.getElementById("emailvalidcheck").innerHTML=xmlhttp.responseText;
		}
	  }
	xmlhttp.open("POST","scripts/user/userfunctions.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("value=CheckEmail;"+email);
	
	/*answer = ajax("scripts/user/userfunctions.php","value=CheckEmail;"+email);
	if (answer == "<img src='images/right.png' width='18' height='18'/> This e-mail adress is valid"){
		enable[7] = false;
	}else{
		enable[7] = true;
	}
	document.getElementById("emailvalidcheck").innerHTML=answer;*/
	if(IsEmpty(email2)) 
   { 
		document.getElementById("emailcheck").innerHTML="";
		enable[8] = false;
		return;
   } else {
	if (email == email2){
			document.getElementById("emailcheck").innerHTML="<img src='images/right.png' width='18' height='18'/>";
			enable[8] = true;
		}else{
			document.getElementById("emailcheck").innerHTML="<img src='images/wrong.png' width='18' height='18'/> The e-mail adresses doesn't match";
			enable[8] = false;
		}
   }
   enableButton2();
}

function emailcheck(email,number){
if (IsEmpty(email))
	{
		document.getElementById("emailvalidcheck"+number).innerHTML="";
		return;
	} 
	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//return xmlhttp.responseText;
			document.getElementById("emailvalidcheck"+number).innerHTML=xmlhttp.responseText;
		}
	  }
	xmlhttp.open("POST","scripts/user/userfunctions.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("value=CheckEmail;"+email);
}

function logincheck()
{
var answer = "";
if (document.login.username.value=="")
  {
  document.getElementById("wrong").innerHTML="";
  return;
  } 
  if (window.XMLHttpRequest)	//ab hier testweise
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	if (xmlhttp.responseText=="right"){
		location.href="sendamessage.php";
	}else{
		if (xmlhttp.responseText=="activate.php"){
			location.href="activate.php";
		}else{
			document.getElementById("wrong").innerHTML=xmlhttp.responseText;
			document.login.password.value = "";
		}
	}
    }
  }
xmlhttp.open("POST","scripts/user/userfunctions.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("value=Login;"+document.login.username.value+";"+document.login.password.value);
  /*answer = ajax("scripts/user/userfunctions.php","value=Login;"+document.login.username.value+", "+document.login.password.value); // bis hier
	if (answer=="right"){
		location.href="sendamessage.php";
	}else{
		if (answer=="activate.php"){
			location.href="activate.php";
		}else{
			document.getElementById("wrong").innerHTML=answer;
			document.login.passwort.value = "";
		}
	}*/
}

function activating(){
	if (document.activate.code.value=="")
	  {
	  document.getElementById("wrong").innerHTML="";
	  return;
	  } 
	  
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		if (xmlhttp.responseText=="right"){
			location.href="sendamessage.php";
		}else{
			document.getElementById("wrong").innerHTML=xmlhttp.responseText;
		}
		}
	  }
	xmlhttp.open("POST","scripts/user/userfunctions.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("value=checkActivate;"+document.activate.code.value);
}

function addEmail(){
	var i = addMail +1;
	addMail = i;
	if (i<=2){
		document.getElementById("moreemail").outerHTML += "<tr id=\"moreemail"+i+"\">" +
					"<td><label for=\"email"+i+"\">E-mail "+i+"</label></td>"+
					"<td><input type=\"text\" name=\"email"+i+"\" id=\""+i+"\" onkeyup=\"emailcheck(this.value, this.id)\" onchange=\"emailcheck(this.value, this.id)\"/></td>"+
					"<td><input type=\"radio\" name=\"primary\" value=\""+i+"\"/></td>"+
					"<td id=\"emailvalidcheck"+i+"\"></td>"+
					"</tr>";
	}else{
		document.getElementById("moreemail"+(i-1)).outerHTML += "<tr id=\"moreemail"+i+"\">" +
					"<td><label for=\"email"+i+"\">E-mail "+i+"</label></td>"+
					"<td><input type=\"text\" name=\"email"+i+"\" id=\""+i+"\" onkeyup=\"emailcheck(this.value, this.id)\" onchange=\"emailcheck(this.value, this.id)\"/></td>"+
					"<td><input type=\"radio\" name=\"primary\" value=\""+i+"\"/></td>"+
					"<td id=\"emailvalidcheck"+i+"\"></td>"+
					"</tr>";
	}
}